package com.ag.lwc.dao;


import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.ag.bean.TbUserContact;
import com.ag.dao.TbUserContactDAO;


public class UserContactDAO extends TbUserContactDAO {
	private static final Log log = LogFactory.getLog(TbUserContactDAO.class);
	/**
	 * 删除用户
	 * @param ucId
	 */
	public void delete(String ucId) {
		log.debug("deleting TbUserContact instance");
		Session session = this.getSessionFactory().openSession();
		Transaction tx = session.beginTransaction();
		try {
			session.createSQLQuery("delete from TB_USER_CONTACT where UC_ID =:ucId").setString("ucId",ucId).executeUpdate();
			tx.commit();
			log.debug("delete successful");
		} catch (RuntimeException re) {
			re.printStackTrace();
			log.error("delete failed", re);
			throw re;
		}
		session.close();
	}
	/**
	 * 查找用户的所有个人通信录
	 * @param uid
	 * @return
	 */
	public  List<TbUserContact> findAllContact(String uid)
	{
		Session session = this.getSessionFactory().openSession();
		List<TbUserContact> result = null;
		
		result = session.createQuery("from TbUserContact t left join fetch t.tbUser u where u.UId =:uid").setString("uid", uid).list();
		if(result!=null)
		System.out.println("用户通信录的数目： "+result.size());
		session.close();
		return result;
	}
	

	/**
	 * 查找用户的某个虚拟组的通信录
	 * @param uid
	 * @param groupId
	 * @return
	 */
	public List<TbUserContact> findContactByGroup(String uid,int groupId)
	{
		Session session = this.getSessionFactory().openSession();
		List<TbUserContact> result = null;
		
		result = session.createQuery("from TbUserContact t left join fetch t.tbUser u left join fetch t.group g where u.UId =:uid and g.groupId =:groupId").setString("uid", uid).setInteger("groupId", new Integer(groupId)).list();
		if(result!=null)
		System.out.println("用户通信录的数目： "+result.size());
		session.close();
		return result;
	}
}
